package acceptanceTests.search;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.Socket;

import domain.items.ForumThread;
import domain.items.Item;
import domain.items.Post;

import server.ForumServer;
import server.WSEPHandler;

public class SearchTestImp implements SearchTestInt {
	
	private boolean _implementedFindSubject  = true;
	private boolean _implementedGetLegalTopicForNewMessage  = true;
	private boolean _implementedRegister  = false;
	private boolean _implementedWriteNewMessage  = true;
	private boolean _implementedDeletePost  = true;
	private boolean _implementedPremote  = true;
	
	private ForumServer _server;
	private WSEPHandler _handler;
	

	public SearchTestImp(ForumServer server){
		this._server = server;
		try{
			Socket s= new Socket("localhost", getPort());
			_handler= new WSEPHandler(s,server);
		}
		catch(IOException e){
		}
	}
	
	private static int getPort() {
		String str="0";
		try {
	        BufferedReader in = new BufferedReader(new FileReader("port.txt"));
	        str = in.readLine();
	        in.close();
	    } catch (IOException e) {
	    }
		return Integer.parseInt(str);
	}

	
	public String findSubject(String subject) {
		return _handler.searchByContent(subject);
	}

	
	public Object getLegalTopicForNewMessage(String userName) {
		_handler.getRootTopic();
		_handler.setCurrentlyViewedItem(_server.getDataRepository().getRootTopic());
		_handler.getItem(1);
		String s1= _handler.getItem(1);
		
		return s1;
	}

	
	public boolean register(String userName,String email,String password) {
		_handler.registerUser(userName, email, password);

		return true;
	}

	
	public Object writeNewMessageReturnsMessage(String subject,String body, Object topic, String userName) {
		Post p= new Post(_handler.get_currentlyViewedItem(),userName, subject, body);
		_handler.addPost(subject, body, userName);
		_handler.getItem(1);
		return p;
	}

	public boolean is_implementedFindSubject() {
		return _implementedFindSubject;
	}

	public void set_implementedFindSubject(boolean findSubject) {
		_implementedFindSubject = findSubject;
	}

	public boolean is_implementedGetLegalTopicForNewMessage() {
		return _implementedGetLegalTopicForNewMessage;
	}

	public void set_implementedGetLegalTopicForNewMessage(
			boolean getLegalTopicForNewMessage) {
		_implementedGetLegalTopicForNewMessage = getLegalTopicForNewMessage;
	}

	public boolean is_implementedRegister() {
		return _implementedRegister;
	}

	public void set_implementedRegister(boolean register) {
		_implementedRegister = register;
	}

	public boolean is_implementedWriteNewMessage() {
		return _implementedWriteNewMessage;
	}

	public void set_implementedWriteNewMessage(boolean writeNewMessage) {
		_implementedWriteNewMessage = writeNewMessage;
	}

	
	public boolean deletePost(Object message) {
		Item i1;
		ForumThread t1;
		_handler.getRootTopic();
		_handler.setCurrentlyViewedItem(_server.getDataRepository().getRootTopic());
		_handler.getItem(1);
		_handler.getItem(1);
		i1=_handler.get_currentlyViewedItem();
		System.out.println(i1.getName());
		t1= (ForumThread)i1;
		_handler.getItem(t1.getPosts().size());

		try{
		_handler.deletePost();
		}
		catch(ClassCastException e){}
		
		return true;
		
	}

	public boolean is_implementedDeletePost() {
		return _implementedDeletePost;
	}

	public void set_implementedDeletePost(boolean deletePost) {
		_implementedDeletePost = deletePost;
	}

	
	public void premoteUser(String username) {
		_handler.promoteUser(username);
		
	}

	public boolean is_implementedPremote() {
		return _implementedPremote;
	}

	public void set_implementedPremote(boolean premote) {
		_implementedPremote = premote;
	}




}
